home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / DeHQX 2.0.0 / source / MyFileSystem.unit < prev    next >
Encoding:
Text File  |  1991-08-23  |  7.4 KB  |  308 lines  |  [TEXT/PJMM]

  1. unit MyFileSystem;
  2. { DeHQX v2.0.0 © Peter Lewis, Aug 1991 }
  3.  
  4. interface
  5.  
  6.     uses
  7.         MyTypes;
  8.  
  9.     const
  10.         PAvailable = fsCurPerm;
  11.         PIn = fsRdPerm;
  12.         POut = fsWrPerm;
  13.         PInOut = fsRdWrPerm;
  14.         PShared = fsRdWrShPerm;
  15.         buf_size = 2048;
  16.         eof_byte = $1A;
  17.  
  18.     type
  19.         bufferArray = packed array[0..buf_size] of byte;
  20.         bufferPtr = ^bufferArray;
  21.         bufferHandle = ^bufferPtr;
  22.         MFSfile = record
  23.                 reading: boolean;
  24.                 rn: integer;
  25.                 buf_len, buf_pos: longInt;
  26.                 eof: boolean;
  27.                 length: longInt;
  28.                 buf: bufferHandle;
  29.             end;
  30.  
  31.     function MFSExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  32.     function MFSDirExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  33.     procedure MFSUniqueName (wdrn: integer; dirID: longInt; var name: str63);
  34.     function MFSOpenIn (wdrn: integer; dirID: longInt; name: str255; var thefile: MFSfile): OSErr;
  35.     function MFSCreate (wdrn: integer; dirID: longInt; name: str255; c, t: OSType): OSErr;
  36.     function MFSDelete (wdrn: integer; dirID: longInt; name: str255): OSErr;
  37.     function MFSOpenOutDF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  38.     function MFSOpenOutRF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  39.     function MFSEof (var thefile: MFSfile): boolean;
  40.     function MFSLength (var thefile: MFSfile): longInt;
  41.     function MFSReadByte (var thefile: MFSfile; var b: byte): OSErr;
  42.     function MFSWriteByte (var thefile: MFSfile; b: byte): OSErr;
  43.     function MFSClose (var thefile: MFSfile): OSErr;
  44.     function MFSOpenDF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  45.     function MFSOpenRF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  46. { perm = fsCurPerm, fsRdPerm, fsWrPerm, fsRdWrPerm, fsRdWrShPerm }
  47.  
  48. implementation
  49.  
  50.     procedure InitTheFile (var thefile: MFSfile);
  51.     begin
  52.         thefile.buf := bufferHandle(NewHandle(buf_size));
  53.     end;
  54.  
  55.     function MFSExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  56.         var
  57.             pb: HParamBlockRec;
  58.     begin
  59.         with pb do begin
  60.             ioNamePtr := @name;
  61.             ioVRefNum := wdrn;
  62.             ioDirID := dirID;
  63.             ioFDirIndex := 0;
  64.         end;
  65.         MFSExists := PBHGetFInfo(@pb, false) = noErr;
  66.     end;
  67.  
  68.     function MFSDirExists (wdrn: integer; dirID: longInt; name: str255): boolean;
  69.         var
  70.             pb: HParamBlockRec;
  71.             oe: OSErr;
  72.     begin
  73.         with pb do begin
  74.             ioNamePtr := @name;
  75.             ioVRefNum := wdrn;
  76.             ioDirID := dirID;
  77.             if name = '' then
  78.                 ioFDirIndex := -1
  79.             else
  80.                 ioFDirIndex := 0;
  81.         end;
  82.         oe := PBGetCatInfo(@pb, false);
  83.         MFSDirExists := (oe = noErr) and (BAND(pb.ioFlAttrib, $0010) <> 0);
  84.     end;
  85.  
  86.     procedure MFSUniqueName (wdrn: integer; dirID: longInt; var name: str63);
  87.         var
  88.             base: str31;
  89.             n: integer;
  90.     begin
  91.         if MFSExists(wdrn, dirID, name) then begin
  92.             base := Concat(Copy(name, 1, 27), '#');
  93.             n := 1;
  94.             repeat
  95.                 name := Concat(base, chr(n div 100 + 48), chr(n div 10 mod 10 + 48), chr(n mod 10 + 48));
  96.                 n := n + 1;
  97.             until not MFSExists(wdrn, dirID, name);
  98.         end;
  99.     end;
  100.  
  101.     function MFSOpenIn (wdrn: integer; dirID: longInt; name: str255; var thefile: MFSfile): OSErr;
  102.     begin
  103.         InitTheFile(thefile);
  104.         with thefile do begin
  105.             reading := true;
  106.             buf_pos := 0;
  107.             buf_len := 0;
  108.             MFSOpenIn := MFSOpenDF(rn, wdrn, dirID, name, PIn);
  109.             if GetEOF(rn, length) <> noErr then
  110.                 length := 0;
  111.             eof := length = 0;
  112.         end;
  113.     end;
  114.  
  115.     function MFSCreate (wdrn: integer; dirID: longInt; name: str255; c, t: OSType): OSErr;
  116.         var
  117.             ooe, oe: integer;
  118.             fi: Finfo;
  119.     begin
  120.         oe := HCreate(wdrn, dirID, name, c, t);
  121.         if oe = dupFNErr then begin
  122.             ooe := HGetFInfo(wdrn, dirID, name, fi);
  123.             oe := HDelete(wdrn, dirID, name);
  124.             oe := HCreate(wdrn, dirID, name, c, t);
  125.             if (oe = noErr) and (ooe = noErr) then begin
  126.                 fi.fdType := t;
  127.                 fi.fdCreator := c;
  128.                 ooe := HSetFInfo(wdrn, dirID, name, fi);
  129.             end;
  130.         end;
  131.         MFSCreate := oe;
  132.     end;
  133.  
  134.     function MFSDelete (wdrn: integer; dirID: longInt; name: str255): OSErr;
  135.     begin
  136.         MFSDelete := HDelete(wdrn, dirID, name);
  137.     end;
  138.  
  139.     function MFSOpenOutDF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  140.         var
  141.             oe: integer;
  142.             fi: fInfo;
  143.     begin
  144.         InitTheFile(thefile);
  145.         with thefile do begin
  146.             reading := false;
  147.             oe := MFSCreate(wdrn, dirID, name, c, t);
  148.             if oe = noErr then
  149.                 oe := MFSOpenDF(rn, wdrn, dirID, name, POut);
  150.             buf_pos := 0;
  151.             buf_len := 0;
  152.             length := 0;
  153.             eof := false;
  154.             MFSOpenOutDF := oe;
  155.         end;
  156.     end;
  157.  
  158.     function MFSOpenOutRF (wdrn: integer; dirID: longInt; name: str255; c, t: OSType; var thefile: MFSfile): OSErr;
  159.         var
  160.             oe: integer;
  161.     begin
  162.         InitTheFile(thefile);
  163.         with thefile do begin
  164.             reading := false;
  165.             oe := MFSCreate(wdrn, dirID, name, c, t);
  166.             if oe = dupFNErr then
  167.                 oe := noErr;
  168.             if oe = noErr then
  169.                 oe := MFSOpenRF(rn, wdrn, dirID, name, POut);
  170.             buf_pos := 0;
  171.             buf_len := 0;
  172.             length := 0;
  173.             eof := false;
  174.             MFSOpenOutRF := oe;
  175.         end;
  176.     end;
  177.  
  178.     function MFSLength (var thefile: MFSfile): longInt;
  179.         var
  180.             l: longInt;
  181.     begin
  182.         MFSLength := thefile.length;
  183.     end;
  184.  
  185.     function MFSEof (var thefile: MFSfile): boolean;
  186.     begin
  187.         MFSEof := thefile.eof;
  188.     end;
  189.  
  190.     function MFSReadByte (var thefile: MFSfile; var b: byte): OSErr;
  191.         var
  192.             oe: OSErr;
  193.         procedure Read;
  194.         begin
  195.             with thefile do begin
  196.                 buf_pos := 0;
  197.                 buf_len := buf_size;
  198.                 oe := FSRead(rn, buf_len, POINTER(buf^));
  199.                 if oe = eofErr then
  200.                     oe := noErr;
  201.                 if buf_len = 0 then
  202.                     oe := eofErr;
  203.                 if oe <> noErr then begin
  204.                     buf_len := 0;
  205.                     eof := true;
  206.                 end;
  207.             end;
  208.         end;
  209.     begin
  210.         with thefile do
  211.             if reading then begin
  212.                 if eof then begin
  213.                     b := eof_byte;
  214.                     MFSReadByte := eofErr;
  215.                 end
  216.                 else begin
  217.                     oe := noErr;
  218.                     if buf_pos = buf_len then
  219.                         Read;
  220.                     MFSReadByte := oe;
  221.                     if oe = noErr then begin
  222.                         b := buf^^[buf_pos];
  223.                         buf_pos := buf_pos + 1;
  224.                         if buf_pos = buf_len then
  225.                             Read;
  226.                     end;
  227.                 end;
  228.             end
  229.             else
  230.                 MFSReadByte := paramErr;
  231.     end;
  232.  
  233.     function Flush (var thefile: MFSfile): OSErr;
  234.         var
  235.             count: longInt;
  236.             oe: integer;
  237.     begin
  238.         with thefile do begin
  239.             count := buf_pos;
  240.             if count = 0 then
  241.                 oe := noErr
  242.             else
  243.                 oe := FSWrite(rn, count, POINTER(buf^));
  244.             if count <> buf_pos then
  245.                 oe := ioErr;
  246.             buf_len := 0;
  247.             buf_pos := 0;
  248.         end;
  249.         Flush := oe;
  250.     end;
  251.  
  252.     function MFSWriteByte (var thefile: MFSfile; b: byte): OSErr;
  253.     begin
  254.         with thefile do
  255.             if not reading then begin
  256.                 buf^^[buf_pos] := b;
  257.                 buf_pos := buf_pos + 1;
  258.                 if buf_pos = buf_size then
  259.                     MFSWriteByte := Flush(thefile)
  260.                 else
  261.                     MFSWriteByte := noErr;
  262.             end
  263.             else
  264.                 MFSWriteByte := paramErr;
  265.     end;
  266.  
  267.     function MFSClose (var thefile: MFSfile): OSErr;
  268.         var
  269.             oe: integer;
  270.     begin
  271.         if not thefile.reading then
  272.             oe := Flush(thefile);
  273.         MFSClose := FSClose(thefile.rn);
  274.         thefile.rn := 0;                { Never close a file twice }
  275.         DisposHandle(handle(thefile.buf));
  276.     end;
  277.  
  278.     function MFSOpenDF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  279.         var
  280.             pb: HParamBlockRec;
  281.     begin
  282.         with pb do begin
  283.             ioNamePtr := @name;
  284.             ioVRefNum := wdrn;
  285.             ioPermssn := perm;
  286.             ioMisc := nil;
  287.             ioDirID := dirID;
  288.             MFSOpenDF := PBHOpen(@pb, false);
  289.             rn := ioRefNum;
  290.         end;
  291.     end;
  292.  
  293.     function MFSOpenRF (var rn: integer; wdrn: integer; dirID: longInt; name: str63; perm: integer): OSErr;
  294.         var
  295.             pb: HParamBlockRec;
  296.     begin
  297.         with pb do begin
  298.             ioNamePtr := @name;
  299.             ioVRefNum := wdrn;
  300.             ioPermssn := perm;
  301.             ioMisc := nil;
  302.             ioDirID := dirID;
  303.             MFSOpenRF := PBHOpenRF(@pb, false);
  304.             rn := ioRefNum;
  305.         end;
  306.     end;
  307.  
  308. end.